package tpase.KeyServer.Controller;

import tpase.KeyServer.Handler.ServerSetupHandler;
import tpase.KeyServer.Handler.ServerStartHandler;
import tpase.KeyServer.Service.Impl.KeyServerSetupServiceImpl;
import tpase.KeyServer.Service.Impl.KeyServerStartServiceImpl;
import tpase.KeyServer.Service.KeyServerSetupService;
import tpase.KeyServer.Service.KeyServerStartService;
import tpase.KeyServer.VO.Address;
import tpase.util.Util;

public class KeyServerController {
    private String paramPath;
    private Address address;
    private Address setupAddress;
    private boolean isDebug;
    private KeyServerSetupService keyServerSetupService;
    private KeyServerStartService keyServerStartService;


    public KeyServerController(String paramPath, String serverId, boolean isDebug) {
        this.paramPath = paramPath;
        this.isDebug = isDebug;
        keyServerSetupService = new KeyServerSetupServiceImpl(serverId);
        keyServerStartService = new KeyServerStartServiceImpl(serverId);
        loadParameters();
    }


    public void start() {
        new Thread(() -> {
            Util.processServerBootstrap(new ServerStartHandler(keyServerStartService, paramPath, address, isDebug), address.getPort());
        }).start();
    }

    public void setup() {
        keyServerSetupService.clear();
//        new Thread(() -> {
            Util.processClientBootstrap(new ServerSetupHandler(keyServerSetupService, paramPath), address, setupAddress);
//        }).start();
    }




    private void loadParameters() {
        String paramName = "/parameters.json";
        String setupName = "/setupParameters.json";
        address = Util.jsonFileToObj(paramPath + paramName, Address.class);
        setupAddress = Util.jsonFileToObj(paramPath + setupName, Address.class);
    }
}
